export default function (time, formatter) {
  time = time || Date.now()
  formatter = formatter || 'y-m-d h:i:s'

  let date
  if (typeof time === 'object') {
    date = time
  } else {
    time = time + ''
    if (/^[0-9]+$/.test(time)) {
      time = parseInt(time)
      time.toString().length === 10 && (time = time * 1000)
    } else {
      time = time.replace(/-/gm, '/')
    }
    date = new Date(time)
  }

  const formatMap = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    w: date.getDay(),
  }
  const dayMap = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']

  return formatter.replace(/(y+|m+|d+|h+|i+|s+|w+)/g, (str, key) => {
    let value = formatMap[key[0]]
    if (key[0] === 'w') {
      return dayMap[value]
    }
    if (value < 10) {
      value = '0' + value
    }
    return value || 0
  })
}
